<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>module | 个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="1.core.js  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485console.log(this); &#x2F;&#x2F; this指">
<meta property="og:type" content="article">
<meta property="og:title" content="module">
<meta property="og:url" content="https://gitee.com/wieldy/bailment-blog/2022/03/29/module/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="1.core.js  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485console.log(this); &#x2F;&#x2F; this指">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-03-29T14:12:00.000Z">
<meta property="article:modified_time" content="2023-04-23T12:11:43.651Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/bailment-blog/atom.xml" title="个人博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/bailment-blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/bailment-blog/css/style.css">

  
    
<link rel="stylesheet" href="/bailment-blog/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/bailment-blog/" id="logo">个人博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/bailment-blog/">Home</a>
        
          <a class="main-nav-link" href="/bailment-blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/bailment-blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/wieldy/bailment-blog"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-module" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/bailment-blog/2022/03/29/module/" class="article-date">
  <time class="dt-published" datetime="2022-03-29T14:12:00.000Z" itemprop="datePublished">2022-03-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      module
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <ul>
<li>1.core.js</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="variable language_">this</span>); <span class="comment">// this指代的是module.exports 不是全局对象</span></span><br><span class="line"><span class="comment">// console.log(global); // window.window   setImmediate  setTimeout  queueMicrotask</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// console.log(Object.keys(process)); // 代码运行在进程中</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// platform   nextTick  chdir   cwd  env  argv</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// node里面可能需要用作工程化， 打包（通过用户的配置来进行解析）</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// process.argv</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(process.<span class="property">argv</span>); <span class="comment">// 前两个参数是固定的 1） 执行的exe  2） 执行的文件    后面两个就是参数</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> userArgs = &#123;&#125;;</span><br><span class="line">process.<span class="property">argv</span>.<span class="title function_">slice</span>(<span class="number">2</span>).<span class="title function_">reduce</span>(<span class="function">(<span class="params">memo, current, index, array</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (current.<span class="title function_">startsWith</span>(<span class="string">&#x27;--&#x27;</span>)) &#123;</span><br><span class="line">        userArgs[current.<span class="title function_">slice</span>(<span class="number">2</span>)] = array[index + <span class="number">1</span>]</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> memo;</span><br><span class="line">&#125;, userArgs);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(userArgs); <span class="comment">// 可以自己处理用户参数 （我们一般不会自己处理）</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// commander 命令行管家   yargs 解析参数   。。。。。</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// console.log(process.env); // 可以获取当前系统的环境变量</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 只是在执行代码的时候 临时的配一个</span></span><br><span class="line"><span class="comment">// windows下可以通过 set name=value 来进行设置</span></span><br><span class="line"><span class="comment">// mac 可以通过 export name=value 来设置</span></span><br><span class="line"><span class="comment">// 配置环境变量 不统一 （cross-env 可以设置不同的系统的环境变量）</span></span><br><span class="line"><span class="comment">// 在开发的时候 .prod.env   .dev.env  ini格式  a=b (核心就是读取这个文件，将文件的内容手动的放到process.env上)</span></span><br><span class="line"><span class="keyword">if</span> (process.<span class="property">env</span>.<span class="property">NODE_ENV</span> === <span class="string">&#x27;production&#x27;</span>) &#123;</span><br><span class="line"></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 服务端才具备操作进程的能力， 在开发的时候可以通过process来去用，在浏览器中 window.procsss = &#123;env:&quot;developement&quot;&#125;</span></span><br><span class="line">process.<span class="title function_">chdir</span>(<span class="string">&#x27;../../../&#x27;</span>); <span class="comment">// 可以更改执行环境路径，用不到</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(process.<span class="title function_">cwd</span>(), __dirname); <span class="comment">// 代码在哪里执行 目录就是那里 </span></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(path.<span class="title function_">resolve</span>())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 这个process是global上的属性 ，可以直接访问 （全局属性）</span></span><br><span class="line"><span class="comment">// require module exports __dirname __ filename (全局属性)</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(process.<span class="property">platform</span>); <span class="comment">// win32 drain  (mac 支持linux命令)  很多的包，开发工具 都不支持windows</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// global.a = 100;// 这种操作尽量不要出现污染全局变量（全局配置 数据库的连接， 统一的方法）</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// node中的微任务 \ 宏任务</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 文件读写  fs.readFile   setImmediate  setTimeout 这些任务都是node中自己实现的</span></span><br><span class="line"><span class="comment">// node中除了 promise（queueMircrotask） 和 process.nextTick之外都是宏任务 </span></span><br><span class="line"></span><br><span class="line"><span class="title class_">Promise</span>.<span class="title function_">resolve</span>().<span class="title function_">then</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;promise&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line">process.<span class="title function_">nextTick</span>(<span class="function">() =&gt;</span> &#123; <span class="comment">// 这里如果递归调用会出现程序卡死</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;nextTick&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 我们代码执行还是要有一个事件环系统 调度异步任务（node中的核心就是libuv） eventLoop</span></span><br><span class="line"><span class="comment">// node中有自己的事件环机制</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 执行结果和浏览器是一致的</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 主栈执行完毕后 会清空nextTick方法，在清空所有的微任务 ， 再去取出一个宏任务执行完毕后一直循环下去</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>);</span><br><span class="line">fs.<span class="title function_">readFile</span>(<span class="string">&#x27;./note.md&#x27;</span>, <span class="string">&#x27;utf8&#x27;</span>, <span class="function">() =&gt;</span> &#123; <span class="comment">// i/o下一个要执行的队列是check阶段</span></span><br><span class="line">    <span class="comment">// 这两个执行时机在主模块中会前后顺序不一致</span></span><br><span class="line">    <span class="title function_">setImmediate</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;setImmediate&#x27;</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123; <span class="comment">// 这两个方法执行时机不同settimeout是在轮训前执行, setImmediate是在轮训之后</span></span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;timer&#x27;</span>)</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// node的事件环会停止 （整个的表现来看和浏览的事件环是一致的）</span></span><br><span class="line"><span class="comment">// 老版本的node中的eventLoop问题 （会清空一个队列再去清空微任务）</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 执行一个宏任务清空nextTick 在清空微任务队列</span></span><br></pre></td></tr></table></figure>

<ul>
<li><p>events</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 模块分为三种 1） 文件名模块  2） 核心fs 模块path，vm...  3) 第三方模块 co 需要安装的</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 发布订阅模块  events 模块  （vue中源码  eventBus  vue3 mitt  redux中subscribe ）</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 发布订阅能用来做什么事？ (可以处理异步逻辑， 处理代码的耦合问题)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">EventEmitter</span> = <span class="built_in">require</span>(<span class="string">&#x27;events&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Girl</span>(<span class="params"></span>) &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">setPrototypeOf</span>(<span class="title class_">Girl</span>.<span class="property"><span class="keyword">prototype</span></span>, <span class="title class_">EventEmitter</span>.<span class="property"><span class="keyword">prototype</span></span>); <span class="comment">// 这是一个什么继承？</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> girl = <span class="keyword">new</span> <span class="title class_">Girl</span></span><br><span class="line"><span class="keyword">const</span> <span class="title function_">shopping</span> = (<span class="params"></span>) =&gt; <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;购物&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> <span class="title function_">cry</span> = (<span class="params"></span>) =&gt; <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;哭&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// newListener 固定的名字, 是在on之前执行的</span></span><br><span class="line"><span class="comment">// 后续订阅的事件 都会将类型传递过来</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> pendding = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">girl.<span class="title function_">on</span>(<span class="string">&#x27;newListener&#x27;</span>, <span class="keyword">function</span> (<span class="params">type</span>) &#123;</span><br><span class="line">    <span class="comment">// 我们期望只要用户订阅了 我就发布</span></span><br><span class="line">    <span class="keyword">if</span> (pendding) <span class="keyword">return</span></span><br><span class="line">    process.<span class="title function_">nextTick</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        girl.<span class="title function_">emit</span>(type)</span><br><span class="line">    &#125;)</span><br><span class="line">    pendding = <span class="literal">true</span>;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// &#123;女生失恋:[shopping,cry]&#125;</span></span><br><span class="line">girl.<span class="title function_">on</span>(<span class="string">&#x27;女生失恋&#x27;</span>, shopping)</span><br><span class="line">girl.<span class="title function_">on</span>(<span class="string">&#x27;女生失恋&#x27;</span>, cry)</span><br><span class="line"></span><br><span class="line"><span class="comment">// girl.off(&#x27;女生失恋&#x27;, cry)</span></span><br><span class="line"><span class="comment">// 我期望触发三次只执行一次</span></span><br><span class="line"><span class="comment">// girl.emit(&#x27;女生失恋&#x27;)</span></span><br><span class="line"><span class="comment">// girl.emit(&#x27;女生失恋&#x27;)</span></span><br><span class="line"><span class="comment">// girl.emit(&#x27;女生失恋&#x27;)</span></span><br><span class="line"><span class="comment">// girl.emit(&#x27;女生失恋&#x27;)</span></span><br><span class="line"><span class="comment">// girl.emit(&#x27;女生失恋&#x27;)</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>event</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">EventEmitter</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">_events</span> = &#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// on emit off once 四个核心的api</span></span><br><span class="line"><span class="title class_">EventEmitter</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">on</span> = <span class="keyword">function</span> (<span class="params">eventName, callback</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="variable language_">this</span>.<span class="property">_events</span>) <span class="variable language_">this</span>.<span class="property">_events</span> = &#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 如果你绑定的不是newListener，触发newListener</span></span><br><span class="line">    <span class="keyword">if</span> (eventName !== <span class="string">&#x27;newListener&#x27;</span>) &#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">emit</span>(<span class="string">&#x27;newListener&#x27;</span>, eventName)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">_events</span>[eventName] = <span class="variable language_">this</span>.<span class="property">_events</span>[eventName] || []</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">_events</span>[eventName].<span class="title function_">push</span>(callback)</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">EventEmitter</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">emit</span> = <span class="keyword">function</span> (<span class="params">eventName, ...args</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="variable language_">this</span>.<span class="property">_events</span>) <span class="variable language_">this</span>.<span class="property">_events</span> = &#123;&#125;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">_events</span>[eventName].<span class="title function_">forEach</span>(<span class="function">(<span class="params">cb</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="title function_">cb</span>(...args)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">EventEmitter</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">off</span> = <span class="keyword">function</span> (<span class="params">eventName, callback</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (!<span class="variable language_">this</span>.<span class="property">_events</span>) <span class="variable language_">this</span>.<span class="property">_events</span> = &#123;&#125;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">_events</span>[eventName] = <span class="variable language_">this</span>.<span class="property">_events</span>[eventName].<span class="title function_">filter</span>(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> item !== callback &amp;&amp; callback !== item.<span class="property">l</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">EventEmitter</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">once</span> = <span class="keyword">function</span> (<span class="params">eventName, callback</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> <span class="title function_">once</span> = (<span class="params">...args</span>) =&gt; &#123;</span><br><span class="line">        <span class="title function_">callback</span>(...args);</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">off</span>(eventName, once);</span><br><span class="line">    &#125;</span><br><span class="line">    once.<span class="property">l</span> = callback</span><br><span class="line">    <span class="variable language_">this</span>.<span class="title function_">on</span>(eventName, once); <span class="comment">// 先绑定函数，执行后在移除</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = <span class="title class_">EventEmitter</span></span><br><span class="line"><span class="comment">// node中源码 比较老没必要全部换成es6语法</span></span><br><span class="line"><span class="comment">// &#123;女生失恋:[shopping,cry]&#125;</span></span><br></pre></td></tr></table></figure></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/wieldy/bailment-blog/2022/03/29/module/" data-id="cljmgj1ex000izc9j7shj6wk3" data-title="module" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/bailment-blog/2022/03/29/http%E6%89%8B%E5%86%99/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          http
        
      </div>
    </a>
  
  
    <a href="/bailment-blog/2022/03/29/koa/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">koa</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/06/">June 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/04/">April 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/03/">March 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/bailment-blog/2023/07/03/%E5%B7%A5%E4%BD%9C%E6%97%A5%E5%BF%97/">工作日志</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/29/payMent/">payMent</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/20/%E7%BD%91%E7%BA%A2%E9%9D%A2%E8%AF%95%E9%A2%98/">原理题</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/19/%E5%89%8D%E7%AB%AF%E5%A4%A7%E5%B1%8F%E8%87%AA%E9%80%82%E5%BA%94%E6%96%B9%E6%B3%95/">前端大屏自适应方法</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/17/10%E4%B8%AAJavaScript%E5%B8%B8%E8%A7%81%E9%AB%98%E7%BA%A7%E7%9F%A5%E8%AF%86%E7%82%B9/">10个JavaScript常见高级知识点</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/bailment-blog/" class="mobile-nav-link">Home</a>
  
    <a href="/bailment-blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/bailment-blog/js/jquery-3.4.1.min.js"></script>



  
<script src="/bailment-blog/fancybox/jquery.fancybox.min.js"></script>




<script src="/bailment-blog/js/script.js"></script>





  </div>
</body>
</html>